/**
 * Created by xmfang on 2014/12/19.
 */
var remoteRequest = require('../util/remoteRequest');
var moment = require('moment');
var config = require('../conf/config');
var constant = config.constant;

exports.sendInvitationMessage = function (inviter, invitees, schedule) {
    var startTime = moment(schedule.timeScope.start), endTime = moment(schedule.timeScope.end);
    var timeScope;

    if (schedule.isFullDay) {
        timeScope = startTime.format('YYYY-MM-DD') + '至' + endTime.format('YYYY-MM-DD');
    } else if (startTime.isSame(endTime, 'day')) {
        timeScope = startTime.format('YYYY-MM-DD') + " " + startTime.format('HH:mm') + '-' + endTime.format('HH:mm');
    } else {
        timeScope = startTime.format('YYYY-MM-DD HH:mm') + '至' + endTime.format('YYYY-MM-DD HH:mm');
    }
    var messageDetails = inviter + '邀请您参加' + getScheduleTypeDescription(schedule.type) + '<br>'
        + '主题：' + schedule.subject + '<br>'
        + '时间：' + timeScope;

    sendMessage(invitees, messageDetails, schedule._id);
};

var getScheduleTypeDescription = function (scheduleType) {
    return scheduleType == constant.scheduleType.MEETING ? '会议' : '任务';
};

exports.sendCancelInvitationMessage = function (invitees, schedule) {
    var messageDetails = '您的' + getScheduleTypeDescription(schedule.type) + '邀请：' + schedule.subject + '<br>'
        + '已被取消';

    sendMessage(invitees, messageDetails, schedule._id);
};

exports.sendCancelScheduleMessage = function (invitees, schedule) {
    var messageDetails = '您参加的' + getScheduleTypeDescription(schedule.type) + '：' + schedule.subject + '<br>'
        + '已被取消';

    sendMessage(invitees, messageDetails, schedule._id);
};

exports.sendCompeletedTaskMessage = function (invitees, schedule, compeleterName) {
    var messageDetails = compeleterName + '完成了任务：' + schedule.subject;
    sendMessage(invitees, messageDetails, schedule._id);
};

exports.sendRestartTaskMessage = function (invitees, schedule, restarterName) {
    var messageDetails = restarterName + '重启了任务：' + schedule.subject;
    sendMessage(invitees, messageDetails, schedule._id);
};

exports.sendAcceptInvitationMessage = function (inviteeId, inviteeName, schedule) {
    var messageDetails = '您的' + getScheduleTypeDescription(schedule.type) + '邀请：' + schedule.subject + '<br>'
        + '已被' + inviteeName + '接受';

    sendMessage([getInviterFromSchedule(schedule, inviteeId)], messageDetails, schedule._id);
};

exports.sendRefuseInvitationMessage = function (inviteeId, inviteeName, reason, schedule) {
    var messageDetails = '您的' + getScheduleTypeDescription(schedule.type) + '邀请：' + schedule.subject + '<br>'
        + '已被' + inviteeName + '拒绝，原因：' + reason;

    sendMessage([getInviterFromSchedule(schedule, inviteeId)], messageDetails, schedule._id);
};

var getInviterFromSchedule = function (schedule, invitee) {
    var attendees = schedule.attendees;
    for(var i = 0; i < attendees.length; i++) {
        if (attendees[i].member == invitee) {
            return attendees[i].inviter;
        }
    }
};

exports.sendScheduleChangeMessage = function (schedule, oldSchedule, memberIds) {
    var hasModify = false;
    var messageDetails = schedule.sponsor + '更新了' + getScheduleTypeDescription(schedule.type) + '：' + oldSchedule.subject;
    if (schedule.subject != oldSchedule.subject) {
        messageDetails = assemMessageDetails(messageDetails, '主题', schedule.subject);
        hasModify = true;
    }
    if (schedule.location != oldSchedule.location) {
        messageDetails = assemMessageDetails(messageDetails, '地点', schedule.location);
        hasModify = true;
    }
    if (moment(schedule.timeScope.start).diff(oldSchedule.timeScope.start) != 0) {
        var startTime = schedule.isFullDay ? moment(schedule.timeScope.start).format('YYYY-MM-DD') : moment(schedule.timeScope.start).format('YYYY-MM-DD HH:mm');
        messageDetails = assemMessageDetails(messageDetails, '开始时间', startTime);
        hasModify = true;
    }
    if (moment(schedule.timeScope.end).diff(oldSchedule.timeScope.end) != 0) {
        var endTime = schedule.isFullDay ? moment(schedule.timeScope.end).format('YYYY-MM-DD') : moment(schedule.timeScope.end).format('YYYY-MM-DD HH:mm');
        messageDetails = assemMessageDetails(messageDetails, '结束时间', endTime);
        hasModify = true;
    }
    if (moment(schedule.deadline).diff(oldSchedule.deadline) != 0) {
        messageDetails = assemMessageDetails(messageDetails, '截止时间', moment(schedule.deadline).format('YYYY-MM-DD'));
        hasModify = true;
    }
    if (schedule.remark != oldSchedule.remark) {
        messageDetails = assemMessageDetails(messageDetails, '备注', schedule.remark);
        hasModify = true;
    }
    if (schedule.isFullDay != oldSchedule.isFullDay) {
        messageDetails = assemMessageDetails(messageDetails, '时间类型', schedule.isFullDay ? '全天' : '非全天');
        hasModify = true;
    }
    if (schedule.repeat != oldSchedule.repeat) {
        var repeatPeriod;
        if (schedule.repeat == constant.period.DAY) repeatPeriod = '按天重复';
        if (schedule.repeat == constant.period.WEEK) repeatPeriod = '按周重复';
        if (schedule.repeat == constant.period.MONTH) repeatPeriod = '按月重复';
        if (schedule.repeat == constant.period.YEAR) repeatPeriod = '按年重复';
        messageDetails = assemMessageDetails(messageDetails, '周期', repeatPeriod);
        hasModify = true;
    }

    if (hasModify) {
        sendMessage(memberIds, messageDetails, schedule._id);
    }
};

var assemMessageDetails = function (messageDetails, changeDescription, newValue) {
    return messageDetails + '<br>' + changeDescription + '更改为“' + newValue + '”。';
};

exports.sendCommentMessage = function (comenter, schedule, members) {
    var messageDetails = comenter + '评论了' + getScheduleTypeDescription(schedule.type) + '：' + schedule.subject + '<br>';
    sendMessage(members, messageDetails, schedule._id);
};

var sendMessage = function (memberIds, messageDetails, scheduleId) {
    remoteRequest.sendMsg({
        users : memberIds,
        msgBody : [{
            title : '您有新的日程动态',
            description : messageDetails,
            picUrl : config.remote[config.currEvn].msgImgUrl,
            url : config.remote[config.currEvn].msgDetailUrl + scheduleId
        }]
    });
};
